﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Common;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.DataAccess
{
    public class PushMsgDataAccess
    {
        #region 单例模式
        private PushMsgDataAccess()
        {
        }

        private static PushMsgDataAccess _instance;
        private static readonly object _syncObject = new object();

        public static PushMsgDataAccess Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new PushMsgDataAccess();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion 单例模式

        #region 个人消息列表

        /// <summary>
        /// 个人消息列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageRows"></param>
        /// <returns></returns>
        public DataSet GetPushMsgList(long uid, int msgtype, int pageIndex, int pageRows)
        {
            DataSet ds = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_pushmsg_getlist";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@msgtype", DbType.Int32, msgtype);
                db.AddInParameter(Procdbcomm, "@pageIndex", DbType.Int32, pageIndex);
                db.AddInParameter(Procdbcomm, "@pageRows", DbType.Int32, pageRows);
                ds = db.ExecuteDataSet(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("我的消息列表-GetPushMsgList" + ex.GetExcetionMessage());
            }
            return ds;
        }
        /// <summary>
        /// 设置消息已读
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="msgid"></param>
        /// <returns></returns>
        public long ReadPushMsg(long uid, long msgid)
        {
            long ret = 1;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_pushmsg_read";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@msgid", DbType.Int64, msgid);
                db.ExecuteNonQuery(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("设置消息已读-ReadPushMsg" + ex.GetExcetionMessage());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 推送绑定设备
        /// </summary>
        /// <returns></returns>
        public long BindMemberPush(long uid,string device_token,int device_type,out string stroutput)
        {
            long ret = -1;
            stroutput = "推送绑定设备失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_memberpushmsg_bind";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@device_token", DbType.String, device_token);
                db.AddInParameter(Procdbcomm, "@device_type", DbType.Int32, device_type);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 18);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("推送绑定设备-BindMemberPush" + ex.GetExcetionMessage());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 取消推送设备绑定
        /// </summary>
        /// <returns></returns>
        public int DelMemberPushBind(long uid)
        {
            int ret = -1;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "delete from ADZ_MemberPushMsg where uid = @uid";
                DbCommand Procdbcomm = db.GetSqlStringCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.ExecuteNonQuery(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("取消推送设备绑定-DelMemberPushBind" + ex.GetExcetionMessage());
                ret = -99;
            }
            return ret;
        }
        #endregion
    }
}
